package genetic.q3;

import java.util.Random;

import genetic.Genotype;

public class RobbyGenotype implements Genotype {

	private double _totalScore;
	private double _score;
	private int _games;
	private board _b;
	private Random r = new Random();
	public int[] instance = new int[243];
	boolean ranalready = false;

	public RobbyGenotype(){
	}
	public RobbyGenotype(int games, board b){
		_b=b;
		for(int i=0; i<instance.length;i++){
			int move =r.nextInt(7);
			if(i%3==2){
				instance[i]=6;
			}
			else instance[i]=move;
		}
		_games=games;
		_totalScore=0;
		_score=0;

	}

	public RobbyGenotype clone(){
		RobbyGenotype g = new RobbyGenotype(_games, _b);
		g.instance=instance;
		return g;
	}

	@Override
	public String toString() {
		String ans = "";
		for (int i=0; i<instance.length; i++){
			ans = ans+instance[i];
		}
		return ans;
	}

	@Override
	public int[] getIntString() {
		// TODO Auto-generated method stub
		return null;
	}

	public double getFitness(){
		if (!ranalready){
			ranalready = true;
			for (int g=0; g<_games;g++){
				_score = runGame();
				_totalScore = _totalScore+ _score;
			}
		}

		return (double)_totalScore/(double)_games;
	}
	private double runGame() {
		_b.startBoard();
		double sum=0;
		for (int i=0; i<200;i++){
			int[] look = _b.look();
			int pos = (look[0]*(int)(Math.pow(3, 4)))+(look[1]*(int)(Math.pow(3, 3)))+(look[2]*(int)(Math.pow(3, 2)))+(look[3]*(int)(Math.pow(3, 1)))+look[4];
			sum = sum + _b.moved(instance[pos]);
		}
		return sum;
	}
}
